package com.lyf.scm.plan.api.controller;

import com.github.pagehelper.PageInfo;
import com.lyf.scm.common.model.ExcelExportDataDTO;
import com.lyf.scm.common.util.ExcelExportUtil;
import com.lyf.scm.plan.api.dto.*;
import com.lyf.scm.plan.domain.service.RequirementPlanService;
import com.rome.arch.core.clientobject.Response;
import com.rome.arch.util.controller.RomeController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 需求计划
 *
 * @author wangchlei
 * @date 2020/10/26
 */
@Slf4j
@Api(tags = "需求计划")
@RequestMapping("/api/v1/requirements")
@RomeController
public class RequirementPlanController {
    @Resource
    private RequirementPlanService requirementPlanService;

    @PostMapping("/listByWeek")
    @ApiOperation(value = "查询列表数据")
    public Response<PageInfo<RequirementPlanInfoDTO>> listWeekBySelective(@RequestBody RequirementPlanDTO requirementPlanDTO) {
        return Response.builderSuccess(requirementPlanService.listWeekBySelective(requirementPlanDTO));
    }

    @PostMapping("/confirmQty")
    @ApiOperation(value = "更新供应链确认量")
    public Response updateConfirmQty(@Validated @RequestBody List<RequirementConfirmationDTO> requirementConfirmationDTOs,@RequestParam("unitType") Long unitType) {
        requirementPlanService.updateConfirmQty(requirementConfirmationDTOs,unitType);
        return Response.builderSuccess(null);
    }

    @PostMapping("/eChartsData")
    @ApiOperation(value = "图表数据接口")
    public Response<EChartsDTO> eChartsData(@RequestBody RequirementPlanDTO requirementPlanDTO) {
        return Response.builderSuccess(requirementPlanService.eChartsData(requirementPlanDTO));
    }

    @PostMapping("/export")
    @ApiOperation(value = "导出")
    public void exportExcel(@RequestBody RequirementPlanDTO requirementPlanDTO, @RequestParam Integer type, HttpServletResponse httpServletResponse) {
        try {
            ExcelExportDataDTO excelExportDataDTO = requirementPlanService.exportExcel(requirementPlanDTO, type);
            ExcelExportUtil.exportExcel(httpServletResponse, excelExportDataDTO);
        } catch (Exception e) {
            log.error("需求计划导出异常", e);
            ExcelExportUtil.handleExceptionResponse(httpServletResponse, e);
        }
    }

    @PostMapping("/demo")
    @ApiOperation(value = "测试composite聚合")
    public void test() {
        requirementPlanService.test();
    }
}
